package com.project.market.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.project.market.db.domain.MarketFootprint;
import com.project.market.db.domain.MarketUser;
import com.project.market.service.AdminFootprintService;
import com.project.market.service.impl.AdminFootprintServiceImpl;
import com.project.market.util.JacksonUtil;
import com.project.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/footprint/*")
public class AdminFootprintController extends HttpServlet {

    AdminFootprintService adminFootprintService = new AdminFootprintServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String op = req.getRequestURI().replace(req.getContextPath() + "/admin/footprint/", "");
        if("list".equals(op)) {
            list(req,resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String userId = req.getParameter("userId");
        String goodsId = req.getParameter("goodsId");

        List<MarketFootprint> list = adminFootprintService.list(page,limit,sort,order,userId,goodsId);
        if(list == null){
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.fail(402,"参数值不对")));
        }else {
            Object ok = ResponseUtil.okList(list);
            String responseBody = JacksonUtil.writeValueAsString(ok);
            resp.getWriter().println(responseBody);
        }
    }
}
